export const map = {
  data() {
    const self = this
    return {
      gpsPath: [],
      roadPage: 1,
      request: true,
      showmap: true,
      center: [118.11827663845497, 24.496937934028],
      plugin: [
        {
          pName: 'MapType',
          enableHighAccuracy: true, //是否使用高精度定位，默认:true
          timeout: 100, //超过10秒后停止定位，默认：无穷大
          maximumAge: 0, //定位结果缓存0毫秒，默认：0
          convert: false, //自动偏移坐标，偏移后的坐标为高德坐标，默认：true
          zoomToAccuracy: true, //定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：f
          extensions: 'all',
          pitch: 75, // 地图俯仰角度，有效范围 0 度- 83 度
          zoom: 18
        }
      ],
      events: {
        click: function(e) {
          self.center = []
          self.circles[0].center = []
          self.marker.position = []
          const lng = e.lnglat.lng
          const lat = e.lnglat.lat
          self.center[0] = lng
          self.center[1] = lat
          self.circles[0].center[0] = lng
          self.circles[0].center[1] = lat
          self.marker.position[0] = lng
          self.marker.position[1] = lat
          self.$set(self.circles[0].center, 0, lng)
          self.$set(self.circles[0].center, 1, lat)
          self.$set(self.marker.position, 0, lng)
          self.$set(self.marker.position, 1, lat)
        },
        init(map) {
          AMapUI.loadUI(['misc/PathSimplifier'], function(PathSimplifier) {
            if (self.gpsPath.length === 0) {
              return
            }
            const marker = new PathSimplifier({
              zIndex: 100,
              //autoSetFitView:false,
              map: map, //所属的地图实例
              getPath: function(pathData) {
                return pathData.path
              },
              getHoverTitle: function(pathData, pathIndex, pointIndex) {
                if (pointIndex >= 0) {
                  //point
                  return (
                    pathData.name +
                    '，点：' +
                    pointIndex +
                    '/' +
                    pathData.path.length
                  )
                }
                return pathData.name + '，点数量' + pathData.path.length
              },
              autoSetFitView: false,
              renderOptions: {
                pathNavigatorStyle: {
                  initRotateDegree: 0,
                  keyPointTolerance: 0,
                  pathTolerance: 1,
                  renderAllPointsIfNumberBelow: -1,
                  width: 24,
                  height: 24,
                  autoRotate: true,
                  lineJoin: 'round',
                  content: 'defaultPathNavigator',
                  fillStyle: '#4f9bc8',
                  strokeStyle: '#519dcb', //'#eeeeee',
                  lineWidth: 4,
                  pathLinePassedStyle: {
                    lineWidth: 6,
                    strokeStyle: 'rgba(8, 126, 196, 1)',
                    borderWidth: 1,
                    borderStyle: '#eeeeee',
                    dirArrowStyle: {
                      stepSpace: 15,
                      strokeStyle: '#0becec'
                    }
                  },
                  keyPointStyle: {
                    radius: 20,
                    fillStyle: '#24d71d',
                    lineWidth: 10
                  }
                }
              }
            })

            window.pathSimplifierIns = marker
            var path = self.gpsPath
            var data = [
              {
                name: '司机轨迹',
                path: path.slice(0, 100)
              }
            ]
            pathSimplifierIns.setData(data)
            //对第一条线路（即索引 0）创建一个巡航器
            var navg1 = marker.createPathNavigator(0, {
              loop: true, //循环播放
              speed: 2000 //巡航速度，单位千米/小时
            })

            pathSimplifierIns.on('pointClick', function(e, info) {
              console.log(
                'Click: ' + info.pathData.points[info.pointIndex].name
              )
            })

            function expandPath() {
              async function doExpand() {
                await self.changeMarker(self.carno)
                if (!self.request) {
                  return false
                }
                var cursor = navg1.getCursor().clone(), //保存巡航器的位置
                  status = navg1.getNaviStatus()
                data[0].path = path.slice(0, (self.roadPage + 1) * 100)
                pathSimplifierIns.setData(data) //延展路径
                //重新建立一个巡航器
                navg1 = pathSimplifierIns.createPathNavigator(0, {
                  //loop: true, //循环播放
                  speed: 2000 //巡航速度，单位千米/小时
                })
                if (status !== 'stop') {
                  navg1.start()
                }
                //恢复巡航器的位置
                if (cursor.idx >= 0) {
                  navg1.moveToPoint(cursor.idx, cursor.tail)
                }
                return true
              }

              if (doExpand()) {
                setTimeout(expandPath, 3000)
              }
            }

            navg1.start()
            expandPath()
          })
        }
      }
    }
  },
  methods: {
    reload() {
      this.showmap = false
      this.$nextTick(() => {
        this.showmap = true
      })
    }
  }
}
